﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using UI.WebPage.Background;
using Model.Config;
using BLL.Config;
namespace Com.Ompchina.Web.admin.conf
{
    public partial class add_term : AddInfoPage, IAddInfo
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && PageAction.Equals("update")) { this.InitSingleInfo(base.GetUpdateID()); }
        }

        #region IAddPage 成员
        /// <summary>
        /// 检查输入的信息
        /// </summary>
        /// <returns></returns>
        public bool CheckInput()
        {
            if (string.IsNullOrEmpty(txtName.Text.Trim())) 
            {
                base.SetMessage(false, "请输入术语名称");
                return false;
            }
            return true;
        }
        /// <summary>
        /// 初始化对象
        /// </summary>
        /// <param name="id"></param>
        public void InitSingleInfo(int id)
        {
            if (id > 0) 
            {
                TermInfo info = new TermBLL().GetTermByID(id);
                if (info.TermID > 0) 
                {                    
                    ddlParent.DefaultSelect = info.ParentTerm.TermID.ToString();
                    txtLabel.Text = info.TermLabel;
                    txtName.Text = info.TermName;
                    txtDescription.Text = info.TermDescription;

                    if (PageAction.Equals("update")) 
                    {
                        hfID.Value = id.ToString();
                    }
                }
            }
        }
        #endregion

        #region 页面私有方法
        /// <summary>
        /// 创建页面输入的术语对象
        /// </summary>
        /// <returns></returns>
        private TermInfo CreateTerm() 
        {
            TermInfo info = new TermInfo();
            info.ParentTerm = new TermInfo();
            info.ParentTerm.TermID = int.Parse(ddlParent.SelectedValue);
            info.TermLabel = txtLabel.Text.Trim();
            info.TermName = txtName.Text.Trim();
            info.TermDescription = txtDescription.Text.Trim();
            info.TermID = int.Parse(hfID.Value);
            return info;
        }
        /// <summary>
        /// 按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.CheckInput()) 
            {
                TermInfo info = this.CreateTerm();
                TermBLL bll = new TermBLL();
                string message=string.Empty;
                if (info.TermID == 0)
                {
                    if (bll.InsertTerm(info, out message) > 0)
                    {
                        base.SetMessage(true, "术语添加成功");
                        base.Redirect("list_term.aspx?action=list" + base.GetQueryStringAdd(), 1);
                    }
                    else
                        base.SetMessage(false, message + "，请稍后重试");
                }
                else 
                {
                    if (bll.UpdateTerm(info, out message))
                    {
                        base.SetMessage(true, "术语修改成功");
                        base.Redirect("list_term.aspx?action=list" + base.GetQueryStringAdd(), 1);
                    }
                    else
                        base.SetMessage(false, message + "，请稍后重试");
                }
            }
        }
        #endregion        
    }
}
